import {createRouter, createWebHistory} from 'vue-router'

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/login',
            name: '登录',
            component: () => import('../views/Login.vue')
        },
        {
            path: '/',
            name: 'layout',
            component: () => import('../views/Layout.vue'),
            redirect: '/home',
            children: [{
                path: '/home',
                name: '首页',
                component: () => import('../views/home/Home.vue'),
            }, {
                path: '/content',
                name: '内容管理',
                children: [{
                    path: 'category',
                    name: '分类管理',
                    component: () => import('../views/content/CategoryList.vue'),
                }, {
                    path: 'question',
                    name: '八股文管理',
                    component: () => import('../views/content/QuestionList.vue'),
                }, {
                    path: 'exam',
                    name: '题库管理',
                    component: () => import('../views/content/ExamQuestionList.vue'),
                }, {
                    path: 'share',
                    name: '经验分享',
                    component: () => import('../views/content/ShareList.vue'),
                }]
            }, {
                path: '/setting',
                name: '设置',
                children: [{
                    path: 'menu',
                    name: '菜单管理',
                    component: () => import('../views/setting/MenuList.vue'),
                }, {
                    path: 'role',
                    name: '角色管理',
                    component: () => import('../views/setting/RoleList.vue'),
                }, {
                    path: 'user',
                    name: '设置用户管理',
                    component: () => import('../views/setting/UserList.vue'),
                }]
            }, {
                path: '/app',
                name: 'app',
                children: [{
                    path: 'userDevice',
                    name: '设备管理',
                    component: () => import('../views/app/UserDeviceList.vue'),
                }, {
                    path: 'role',
                    name: '角11色管理',
                    component: () => import('../views/setting/RoleList.vue'),
                }, {
                    path: 'user',
                    name: 'app用户管理',
                    component: () => import('../views/app/UserList.vue'),
                }]
            },]
        }
    ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
    const userInfo = sessionStorage.getItem("userInfo");
    if (!userInfo && to.path !== "/login") {
        router.push("/login");
    }
    next();
})

export default router;
